// copyright 1993 Michael B. Johnson; some portions copyright 1994, MIT
// see COPYRIGHT for reuse legalities
//

#import "RIBDisk.h"
#import <stdio.h>


@implementation RIBDisk


+ initialize { return [RIBDisk setVersion:1], self; }

- (BOOL)hasBoundingBox { return YES; }

- init
{
  [super init];

  height = 1.0;
  radius = 1.0;
  
  return self;
}

- setHeight:(RtFloat)newHeight 
     radius:(RtFloat)newRadius 
     thetaMax:(RtFloat)newThetaMax 
     n:(int)newN tokens:(RtToken *)newTokens parms:(RtPointer *)newParms archiveVector:(char **)newArchiveVector
     printfTypeVector:(int *)newPrintfTypeVector printfNVector:(int *)newPrintfNVector
{  
   height = newHeight;
   radius = newRadius;
   thetaMax = newThetaMax;
   [self setN:newN tokens:newTokens parms:newParms archiveVector:newArchiveVector printfTypeVector:newPrintfTypeVector printfNVector:newPrintfNVector];

   dirtyBoundingBox = TRUE;
   return self;
}

- setHeight:(RtFloat)newHeight { height = newHeight; dirtyBoundingBox = TRUE; return self; }
- setRadius:(RtFloat)newRadius { radius = newRadius; dirtyBoundingBox = TRUE; return self; }

- (RtFloat)height { return height; }
- (RtFloat)radius { return radius; }


- calculateBoundingBoxStartingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
{
  if (thetaMax >= 180.0)
  {   boundingBox[0] =  -1.0 * radius;
  }
  else  // less than 180
  {  if (thetaMax >= 90.0)
     {  boundingBox[0] =  -1.0 * radius * (RtFloat)sin((double)(toRadians((thetaMax - 90.0))));
     }
     else
     {  //boundingBox[0] =  radius * (RtFloat)sin((double)(toRadians(thetaMax)));
        boundingBox[0] = 0.0;  // this is not true if zMax or abs(zMin) is less than radius
     }
  }
  boundingBox[1] = radius;

  if (thetaMax >= 270.0)
  {  boundingBox[2] = -1.0 * radius;
     boundingBox[3] = radius;
  }
  else
  {  if (thetaMax >= 180.0)
     {  boundingBox[2] = radius * sin((double)(toRadians((thetaMax))));
        boundingBox[3] = radius;
     }
     else
     {  if (thetaMax >= 90.0)
        {  boundingBox[2] = 0.0;
           boundingBox[3] = radius;
        }
        else
        {  boundingBox[2] = 0.0;
           boundingBox[3] = radius * sin((double)(toRadians((thetaMax))));
        }
     }
  }

  if (height < 0.0)
  {  boundingBox[5] = 0.0;
     boundingBox[4] = height;
  }
  else
  {  boundingBox[4] = 0.0;
     boundingBox[5] = height;
  }

  dirtyBoundingBox = FALSE; 

  return self;
}


- (BOOL)isMoot
{
  if (!radius) { return YES; }

  return [super isMoot];
}

- (BOOL)theSameAs:otherRIBCommand
{
  if (radius != [otherRIBCommand radius])
  {  return NO;
  }
  if (height != [otherRIBCommand height])
  {  return NO;
  }

  return [super theSameAs:otherRIBCommand];
}

- renderSelf:(WW3DCamera *)camera  startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
{
  RiDiskV(height, radius, thetaMax, n, tokens, parms);

  return self;
}

- (unsigned long int)maxSampleBandwidth {  return ([super maxSampleBandwidth] + (unsigned long int)(3 * sizeof(RtFloat))); }

- (BOOL)isLerpable { return YES; }

// note: because we've made the WWSampleList "safe" for having
// multiple samples with the same data, it's perfectly valid to return
// yourself or b
- lerpWith:b by:(float)uValue
{
   id      newMe = nil;
   RtFloat radiusA, heightA, thetaMaxA;
   RtFloat radiusB, heightB, thetaMaxB;


   if (([self class] != [b class]) || (uValue <= 0.0))
   {  return self;
   }

   if (uValue >= 1.0)
   {  return b;
   }

   newMe = [super lerpWith:b by:uValue]; // this makes a copy for us

   // okay, now do the specific stuff for this class
   radiusA = [self radius];
   radiusB = [b radius];
   [newMe setRadius:(radiusA + ((radiusB - radiusA) * uValue))]; 
   heightA = [self height];
   heightB = [b height];
   [newMe setHeight:(heightA + ((heightB - heightA) * uValue))];
   thetaMaxA = [self thetaMax];
   thetaMaxB = [b thetaMax];
   [newMe setThetaMax:(thetaMaxA + ((thetaMaxB - thetaMaxA) * uValue))];
 
   return newMe;
}


- lerpSelfWith:b by:(float)uValue
{
   RtFloat radiusA, heightA, thetaMaxA;
   RtFloat radiusB, heightB, thetaMaxB;


   if (([self class] != [b class]) || (uValue <= 0.0))
   {  return self;
   }

   if (uValue >= 1.0)
   {  return b;
   }

   [super lerpSelfWith:b by:uValue]; // this makes a copy for us

   // okay, now do the specific stuff for this class
   radiusA = [self radius];
   radiusB = [b radius];
   [self setRadius:(radiusA + ((radiusB - radiusA) * uValue))]; 
   heightA = [self height];
   heightB = [b height];
   [self setHeight:(heightA + ((heightB - heightA) * uValue))];
   thetaMaxA = [self thetaMax];
   thetaMaxB = [b thetaMax];
   [self setThetaMax:(thetaMaxA + ((thetaMaxB - thetaMaxA) * uValue))];

   return self;
}

- writeEve:(NXStream *)stream atTabLevel:(int)tab
{
   int  i;


   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }
   NXPrintf(stream, "Disk %f %f %f ", height, radius, thetaMax);
   [super writeParameterList:stream];
   return self;
}

- writeInventorAtTime:(float)currentTime to:(NXStream *)stream atTabLevel:(int)tab
{
   RtFloat umax;
   RtFloat uknots[12];
   RtFloat uk[12] = {0.0, 0.0, 0.0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1.0, 1.0, 1.0};
   RtFloat vknots[4] = {0.0, 0.0, 1.0, 1.0};
   RtFloat xuspline[9][2] = {{1.0, 0.0}, {M_SQRT1_2, -M_SQRT1_2}, {0.0, -1.0}, {-M_SQRT1_2, -M_SQRT1_2}, 
                             {-1.0, 0.0}, {-M_SQRT1_2, M_SQRT1_2},  {0.0, 1.0}, {M_SQRT1_2, M_SQRT1_2}, {1.0, 0.0}};
   RtFloat yuspline[9][2] = {{0.0, 1.0}, {M_SQRT1_2, M_SQRT1_2}, {1.0, 0.0}, {M_SQRT1_2, -M_SQRT1_2}, {0.0, -1.0}, 
  	                     {-M_SQRT1_2, -M_SQRT1_2}, {-1.0, 0.0}, {-M_SQRT1_2, M_SQRT1_2}, {0.0, 1.0}};
   RtFloat zwuspline[9] = {1.0, M_SQRT1_2, 1.0, M_SQRT1_2, 1.0, M_SQRT1_2, 1.0, M_SQRT1_2, 1.0};
   int i, j, k;
   RtPoint point1, point2;


   point1[0] = [self radius];
   point1[1] = 0.0;
   point1[2] = [self height];
   point2[0] = 0.0;
   point2[1] = 0.0;
   point2[2] = [self height];

   umax = [self thetaMax] / 360.0;
   for (i=0; i<12; i++)
   {  uknots[i] = uk[i] / umax;
   }

   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }
   NXPrintf(stream, "# ");
   [self writeEve:stream atTabLevel:0];
   NXPrintf(stream, "\n");

   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }
   NXPrintf(stream, "# since Inventor doesn't directly support quadrics in any terribly useful way (sigh), we turn it into a NURBS surface:\n");

   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }
   NXPrintf(stream, "Separator {\n");

   for (i = 0; i < (tab+1); i++)  {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "Coordinate4 {\n");
   for (i = 0; i < (tab+2); i++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "point [");

   for (j=0; j<9; j++)
   {  if (!j)
      {  NXPrintf(stream, 
		  "%f %f %f %f,\n", 
		  (point1[0] * xuspline[j][0] + point1[1] * xuspline[j][1]),
		  (point1[0] * yuspline[j][0] + point1[1] * yuspline[j][1]),
		  (point1[2] * zwuspline[j]),
		  (zwuspline[j]));
      }
      else
      {  NXPrintf(stream, 
		  "       %f %f %f %f,\n", 
		  (point1[0] * xuspline[j][0] + point1[1] * xuspline[j][1]),
		  (point1[0] * yuspline[j][0] + point1[1] * yuspline[j][1]),
		  (point1[2] * zwuspline[j]),
		  (zwuspline[j]));
      }
      for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
   }

   for (j=0; j<8; j++)
   {  NXPrintf(stream, 
	       "       %f %f %f %f,\n", 
	       (point2[0] * xuspline[j][0] + point2[1] * xuspline[j][1]),
	       (point2[0] * yuspline[j][0] + point2[1] * yuspline[j][1]),
	       (point2[2] * zwuspline[j]),
	       (zwuspline[j]));
      for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
   }
   NXPrintf(stream, 
	    "       %f %f %f %f]\n", 
	    (point2[0] * xuspline[j][0] + point2[1] * xuspline[j][1]),
	    (point2[0] * yuspline[j][0] + point2[1] * yuspline[j][1]),
	    (point2[2] * zwuspline[j]),
	    (zwuspline[j]));

   for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "}\n");

   for (k = 0; k < (tab+1); k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "NurbsSurface {\n");
   for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "numUControlPoints 9\n");
   for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "numVControlPoints 2\n");

   for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "uKnotVector [");
   for (i=0; i<11; i++)
   {	NXPrintf(stream, "%f, ", uknots[i]);
   }
   NXPrintf(stream, "%f]\n", uknots[i]);

   for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "vKnotVector [");
   for (i=0; i<3; i++)
   {	NXPrintf(stream, "%f, ", vknots[i]);
   }
   NXPrintf(stream, "%f]\n", vknots[i]);
   for (k = 0; k < (tab+1); k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "}\n");
   for (k = 0; k < tab; k++) {  NXPrintf(stream, "\t"); }
   NXPrintf(stream, "}\n");

   return self;
}

#define typeVector "ff"
#define typeValues &height, &radius

- read:(NXTypedStream*)stream 
{
    int version;
    [super read:stream];

    version = NXTypedStreamClassVersion(stream,"RIBDisk");
    if (version == 0) NXReadTypes(stream,"i",&version), version=1;
    if (version == 1)
    {  NXReadTypes(stream,typeVector,typeValues);
    } 
    else 
    {
    }
    return self;
}

- write:(NXTypedStream*)stream 
{
    [super write:stream];

    NXWriteTypes(stream,typeVector, typeValues);

    return self;
}


@end
